<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Pagination{

	var $base_url						= '';
	var $num_links						=  3;
	var $cur_page	 					=  0;
	var $first_link   						= '&lsaquo; First';
	var $next_link						= 'Next';
	var $prev_link						= 'Prev';
	var $last_link							= 'Last &rsaquo;';
	var $full_tag_open				= '';
	var $full_tag_close				= '';
	var $first_tag_open				= '';
	var $first_tag_close				= '&nbsp;';
	var $last_tag_open				= '&nbsp;';
	var $last_tag_close				= '';
	var $cur_tag_open				= '&nbsp;<strong>';
	var $cur_tag_close				= '</strong>';
	var $next_tag_open				= '&nbsp;';
	var $next_tag_close				= '&nbsp;';
	var $prev_tag_open				= '&nbsp;';
	var $prev_tag_close				= '';
	var $num_tag_open				= '&nbsp;';
	var $num_tag_close				= '';
	var $page_query_string			= FALSE;
	var $query_string_segment	= 'per_page';
	var $num_pages 					= '';
	var $type 								= 'normal';
	var $ajaxfn							= 'goPage';

	
	function __construct( $params = array() )
	{
		if ( count( $params ) > 0 )
		{
			foreach ( $params as $key => $val )
			{
				$this->$key = $val;
			}
		}
	}
	
	/**
	 * 创建连接
	 *
	 * @return string
	 */
	function create_links(){

		//总页数
		$num_pages = $this->num_pages;

		//如果总页数为0，返回空
		if ( $num_pages == 1 || $num_pages == 0 )
		{
			return '';
		}

		//显示前后连接数
		$this->num_links = (int)$this->num_links;
		
		if( $this->num_links < 1 )
		{
			show_error( '您设置的连接数必须大于等于1' );
		}
				
		if ( !is_numeric( $this->cur_page ) )
		{
			$this->cur_page = 1;
		}

		if( $this->cur_page<=0 )
		{
			$this->cur_page = 1;
		}

		if( $this->cur_page>$num_pages )
		{
			$this->cur_page = $num_pages;
		}

		//总连接数
		$uri_page_number = $this->cur_page;

		// 计算开始和结束
		$start = ( ( $this->cur_page - $this->num_links ) > 0 ) ? $this->cur_page - ( $this->num_links - 1 ) : 1;
		$end   = ( ( $this->cur_page + $this->num_links ) < $num_pages ) ? $this->cur_page + $this->num_links : $num_pages;
		
		if( $this->type == 'ajax' )
		{
			$this->base_url = $this->base_url . '&ajax=true';
		}
		else
		{
			$this->base_url = $this->base_url . '&page=';
		}
		
  		//开始输出
		$output = '';

		//First 连接
		if  ( $this->cur_page > ( $this->num_links + 1 ) )
		{
			if( $this->type == 'ajax' )
			{
				$output .= $this->first_tag_open.'<a href="#" onclick="'.$this->ajaxfn.'([\''.$this->base_url.'\',1])">'.$this->first_link.'</a>'.$this->first_tag_close;
			}
			else 
			{
				$output .= $this->first_tag_open.'<a href="'.$this->base_url.'1'.'">'.$this->first_link.'</a>'.$this->first_tag_close;
			}
		}

		//previous 连接
		if  ( $this->cur_page != 1 )
		{
			$i = $this->cur_page - 1;
			if ($i == 0) $i = '';
			if( $this->type == 'ajax' )
			{
				$output .= $this->prev_tag_open.'<a href="#" onclick="'.$this->ajaxfn.'([\''.$this->base_url.'\','.$i.'])">'.$this->prev_link.'</a>'.$this->prev_tag_close;
			}
			else 
			{
				$output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
			}
		}

		//中间连接
		for ( $loop = $start -1; $loop <= $end; $loop++ )
		{
			if ($loop > 0)
			{
				if ( $this->cur_page == $loop )
				{
					$output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
				}
				else
				{
					$n = ( $loop == 0 ) ? '' : $loop;
					if( $this->type == 'ajax' )
					{
						$output .= $this->num_tag_open.'<a href="#" onclick="'.$this->ajaxfn.'([\''.$this->base_url.'\','.$n.'])">'.$loop.'</a>'.$this->num_tag_close;
					}
					else 
					{
						$output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
					}
				}
			}
		}

		//"next" 链接
		if( $this->cur_page < $num_pages )
		{
			if( $this->type == 'ajax' )
			{
				$output .= $this->next_tag_open.'<a href="#" onclick="'.$this->ajaxfn.'([\''.$this->base_url.'\','.($this->cur_page +1).'])">'.$this->next_link.'</a>'.$this->next_tag_close;
			}
			else 
			{
				$output .= $this->next_tag_open.'<a href="'.$this->base_url.( $this->cur_page +1 ).'">'.$this->next_link.'</a>'.$this->next_tag_close;
			}
		}

		//Last 连接
		if ( ( $this->cur_page + $this->num_links ) < $num_pages ){
			if( $this->type == 'ajax' )
			{
				$output .= $this->last_tag_open.'<a href="#" onclick="'.$this->ajaxfn.'([\''.$this->base_url.'\','.$num_pages.'])">'.$this->last_link.'</a>'.$this->last_tag_close;
			}
			else 
			{
				$output .= $this->last_tag_open.'<a href="'.$this->base_url.$num_pages.'">'.$this->last_link.'</a>'.$this->last_tag_close;
			}
		}

		$output = preg_replace("#([^:])//+#", "\\1/", $output);

		$output = $this->full_tag_open.$output.$this->full_tag_close;
		
		return $output;		
	}
}